import requests
from bs4 import BeautifulSoup as bs
from my_fake_useragent import UserAgent
import random
import time


ua = UserAgent()

def get_ip_list(url):
    headers = {
        'Accept-Language':'zh-CN,zh;q=0.8',
        'Connection':'keep-alive',
        "Host":"www.xicidaili.com",
        "Upgrade-Insecure-Requests":"1",
        "User-Agent": ua.random()
    }
    res = requests.get(url, headers=headers)
    soup = bs(res.text,'html.parser')
    ips = soup.find(id='ip_list').find_all('tr')
    ip_list = []
    for i in range(1,len(ips)):
        ip_info = ips[i]
        tds = ip_info.find_all('td')
        ip_list.append(tds[1].text+':'+tds[2].text)
    print('代理列表抓取成功')
    # print(ip_list) # 输出ip列表
    return ip_list

def get_random_ip(ip_list):
    proxy_list = []
    for ip in ip_list:
        proxy_list.append('http://'+ip)
    proxy_ip = random.choice(proxy_list)
    proxies = {
        'http': proxy_ip
    }
    print('设置成功')
    return proxies

def get_url(num):
    try:
        urls = ['http://www.xicidaili.com/nn/{}'.format(number)
               for number in range(1,num)]
        for url in urls:
            list.append(get_ip_list(url))
        result = get_random_ip(list)
        print(result)
        return result
    except IOError:
        print('error')


if __name__ == '__main__':
    start = time.time()
    try:
        urls = ['http://www.xicidaili.com/nn/{}'.format(number)
               for number in range(1,10)]
        for url in urls:
            list = get_ip_list(url)
            result = get_random_ip(list)
            print(result)
    except IOError:
        print('error')

    end = time.time()
    print('总时间：',end - start)